<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Plus } from '@element-plus/icons-vue'
import tagSelect from '@/components/TagSelect.vue'
import idNumberEdit from '@/components/IdNumberEdit.vue'
import { useUserStore } from '@/stores'
import { uploadPicture, userUpdateInfoService } from '@/api/user'
import { getPersonPostListServer, deletePostServer } from '@/api/postList'

const userStore = useUserStore()
const router = useRouter()
const changeFlag = ref(true)

onMounted(() => {
  info.value = userStore.user
  getPersonPostList()
})

const postList = ref([])
const getPersonPostList = async () => {
  const res = await getPersonPostListServer(0, 0)
  postList.value = res.data.data.records
}

const changeClick = () => {
  changeFlag.value = !changeFlag.value
}
const updateClick = async () => {
  const res = await userUpdateInfoService(info.value)
  ElMessage.success(res.data.msg)
  changeFlag.value = !changeFlag.value
}

const idNumberEditRef = ref()
// const idNumberAdd = () => {
//   idNumberEditRef.value.open()
// }

const info = ref({
  name: '',
  phone: '',
  className: '',
  gender: 0,
  avatar: ''
})

const options = ref([
  { id: 0, value: '未知' },
  { id: 1, value: '男' },
  { id: 2, value: '女' }
])

const imgGet = async (e) => {
  const rep = await uploadPicture(e.raw)
  ElMessage.success(rep.data.msg)
  await userStore.getUser()
  info.value = userStore.user
}

const goDetail = (item) => {
  router.push(`/postList/detail?item=${JSON.stringify(item)}`)
}

const deletePostList = async (id) => {
  await ElMessageBox.confirm('你确认删除该帖子吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await deletePostServer(id)
  await getPersonPostList()
}
</script>

<template>
  <page-container title="个人资料">
    <template #extra> </template>

    <div class="infoBox">
      <el-descriptions
        style="margin-top: 10px"
        class="info"
        :column="3"
        direction="vertical"
        border
      >
        <el-descriptions-item
          :rowspan="2"
          label="头像"
          align="center"
          width="25%"
        >
          <div
            style="
              position: relative;
              width: 100px;
              height: 100px;
              margin: auto;
            "
          >
            <el-upload
              class="avatar-uploader"
              action="#"
              :auto-upload="false"
              :on-change="imgGet"
              :show-file-list="false"
              style="margin-top: 9px"
              :disabled="changeFlag"
            >
              <img v-if="info.avatar" :src="info.avatar" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="昵称" width="25%">
          <span v-if="changeFlag">{{ info.name }}</span>
          <el-input v-else placeholder="请输入昵称" v-model="info.name" />
        </el-descriptions-item>
        <el-descriptions-item label="手机号" width="25%">
          <span>{{ info.phone }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="性别" width="25%">
          <span v-if="changeFlag">{{
            options.find((item) => {
              return item.id === info.gender
            }).value
          }}</span>
          <el-select v-model="info.gender" placeholder="Select" v-else>
            <el-option
              v-for="item in options"
              :key="item.id"
              :value="item.id"
              :label="item.value"
            />
          </el-select>
        </el-descriptions-item>
        <el-descriptions-item label="班级" width="25%">
          <span v-if="changeFlag">{{ info.className }}</span>
          <el-input v-else placeholder="请输入班级" v-model="info.className" />
        </el-descriptions-item>
        <!-- <el-descriptions-item label="宿舍" width="25%">
          <span v-if="changeFlag">西一666</span>
          <el-input v-else placeholder="请输入宿舍" />
        </el-descriptions-item>
        <el-descriptions-item label="是否已实名" width="25%">
          <span v-if="changeFlag">否</span>
          <a href="javascript:;" v-else @click="idNumberAdd">点此实名</a>
        </el-descriptions-item> -->
      </el-descriptions>

      <div class="postList">
        <div>发布帖子列表</div>
        <ul class="postListBox">
          <li
            class="postListItem"
            @click="goDetail(item)"
            v-for="item in postList"
            :key="item.postId"
          >
            <img :src="item.imageUrls[0]" alt="" />
            <div class="postBox">
              <span class="postListClass">
                <tagSelect :index="item.feed" />

                <el-button
                  type="danger"
                  class="deleteButton"
                  @click.stop="deletePostList(item.postId)"
                  >删除</el-button
                >
              </span>
              <span class="title">{{ item.title }}</span>
            </div>
          </li>
        </ul>
      </div>
    </div>
    <div class="button">
      <el-button type="primary" v-if="changeFlag" @click="changeClick"
        >修改资料</el-button
      >
      <el-button type="warning" v-else @click="updateClick">确定修改</el-button>
    </div>
  </page-container>

  <idNumberEdit ref="idNumberEditRef"></idNumberEdit>
</template>

<style scoped>
.postListClass {
  display: flex;
  justify-content: space-between;
  .deleteButton {
    height: 24px;
    padding: 0 9px;
    font-size: 12px;
    line-height: 1;
    border-width: 1px;
    border-style: solid;
    border-radius: 4px;
    box-sizing: border-box;
    white-space: nowrap;
    --el-icon-size: 14px;
  }
}
.block {
  display: inline-flex;
  border-right: solid 1px var(--el-border-color);
  flex: 1;
}
.button {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.infoBox {
  display: flex;
  .info {
    flex: 1;
    margin-right: 20px;
  }
  .postList {
    width: 25%;
    background-color: #fff;
    padding: 10px;
    border-radius: 15px;
    .postListBox {
      width: 100%;
      height: 50vh;
      overflow-y: scroll;
      padding: 0;
      .postListItem {
        display: flex;
        padding: 10px;
        border: 1px solid var(--el-border-color);
        border-radius: 5px;
        margin-bottom: 20px;
        > img {
          width: 40%;
          aspect-ratio: 16/9;
          margin-right: 10px;
        }
        .postBox {
          flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          .title {
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            line-clamp: 2;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }
      }
    }
  }
}
.shadow {
  position: absolute;
  top: 0;
  width: 100px;
  height: 100px;
  background-color: rgba(0, 0, 0, 0.5);
  border-radius: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 30px;
  opacity: 0;
  transition: all 0.3s;
  &:hover {
    opacity: 1;
  }
}
.avatar,
.avatar-uploader-icon {
  width: 100px;
  height: 100px;
}
</style>
